package com.xjscrm.console.chat.core.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjscrm.common.entity.WwCustomer;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RPullCustomerMsg;
import com.xjscrm.console.chat.frame.send.SPullCustomerResultMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.dto.chat.ChatCustomerDTO;
import com.xjscrm.console.service.customer.chat.ChatTalkRecordService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
import com.xjscrm.console.service.customer.wwCustomer.WwCustomerService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatPullCustomerMsgHandler extends ChatBaseMsgHandler<RPullCustomerMsg> {

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;
    @Autowired
    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;
    @Autowired
    private ChatTalkRecordService chatTalkRecordService;

    @Autowired
    private WwCustomerService customerService;


    @Override
    public SResultMsg doHandle(RPullCustomerMsg content, ChannelHandlerContext ctx) {

        SPullCustomerResultMsg pullCustomerResultMsg = new SPullCustomerResultMsg();

        Page<ChatCustomerDTO> result = new Page<>(content.getSize(), content.getCurrent());
        pullCustomerResultMsg.setPage(result);

        try {
            Long merId = content.getMerId();
            //
            Page<WwCustomer> customerList = customerService.pageFromChat(merId, content);
            if (ObjectUtils.isEmpty(customerList.getRecords())) {
                return pullCustomerResultMsg;
            }
            List<ChatCustomerDTO> resultRecords = new ArrayList<>();
            for (WwCustomer record : customerList.getRecords()) {
                ChatCustomerDTO chatCustomerDTO = new ChatCustomerDTO();

                chatCustomerDTO.setHeadImgUrl(record.getHeadImgUrl());
                chatCustomerDTO.setName(record.getName());
                chatCustomerDTO.setWxId(record.getWxid());

                resultRecords.add(chatCustomerDTO);
            }
            result.setRecords(resultRecords);
        } catch (Exception e) {
            log.error("拉取机器人客户失败", e);
            throw new BusinessException("拉取机器人客户失败");
        }
        return pullCustomerResultMsg;
    }

    @Override
    public boolean checkMsg(RPullCustomerMsg baseMsg) {
        return super.checkMsg(baseMsg) || !Objects.nonNull(baseMsg.getDeviceUniqueId());
    }

    @Override
    public Class<RPullCustomerMsg> msgClass() {
        return RPullCustomerMsg.class;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_pull_customer;
    }
}
